BINDING_SITES=pho4 pho4g pho4t met4 met31

FAMILIES=pho4 met4 met31
FAMILIES_ALL=pho_all met_all
MATRICES=pho4 pho4g pho4t met4 met31

usage:
	@echo "usage: make [-OPT='options'] target"
	@echo "implemented targets"
	@perl -ne 'if (/^(\S+:)/){ print "\t"; print }' makefile


################################################################################################
#  MATRICES
################################################################################################


retrieve_each_seq:
	for family in ${FAMILIES} ; do \
		echo "retrieving $${family} upstream sequences ..." ; \
		retrieve-seq -i $${family}.family -type upstream -org Saccharomyces_cerevisiae \
		-from -1 -to -800 -format wc -nocomments -label orf_gene -o $${family}.wc ; \
	done

retrieve_binding_sites:
	retrieve-seq -q ${GENE} -type upstream -org Saccharomyces_cerevisiae \
	-from ${min} -to ${max} -format wc -nocomments -label orf_gene -o ${GENE_OUT}

create_matrices_from_genes:
	@for family in ${FAMILIES} ; do \
		echo "create matrix $${family}P.matrice from family $${family}.family ..." ; \
		consensus -f $${family}.wc -L 10 -A a:t c:g -c2 -N 10 \
		| matrix-from-consensus -o $${family}P.matrice ; \
	done

retrieve_all_binding_sites_for_pho4:
	@echo "\nretrieve all binding sites for pho4...\n"
	@make retrieve_binding_sites GENE=PHO5 min=-260 max=-242 GENE_OUT=test; cp test pho4.bs; mv test pho4g.bs
	@make retrieve_binding_sites GENE=PHO5 min=-202 max=-182 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO5 min=-368 max=-349 GENE_OUT=test; cat test >> pho4.bs; cp test pho4t.bs; rm test
	@make retrieve_binding_sites GENE=PHO8 min=-540 max=-522 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO8 min=-736 max=-718 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test
	@make retrieve_binding_sites GENE=PHO81 min=-350 max=-332 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO84 min=-421 max=-403 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO84 min=-442 max=-425 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO84 min=-884 max=-869 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO84 min=-267 max=-250 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test
	@make retrieve_binding_sites GENE=PHO84 min=-592 max=-575 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test
#   suppl sites:
	@make retrieve_binding_sites GENE=PHO11 min=-290 max=-272 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO89 min=-330 max=-312 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO86 min=-197 max=-179 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=SPL2 min=-368 max=-350 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=SPL2 min=-154 max=-136 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=SPL2 min=-97 max=-79 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=SPL2 min=-38 max=-20 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4g.bs; rm test
	@make retrieve_binding_sites GENE=PHO11 min=-423 max=-405 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test
	@make retrieve_binding_sites GENE=PHO89 min=-470 max=-452 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test
	@make retrieve_binding_sites GENE=PHO86 min=-503 max=-485 GENE_OUT=test; cat test >> pho4.bs; cat test >> pho4t.bs; rm test

retrieve_all_binding_sites_for_met4:
	@echo "\nretrieve all binding sites for met4...\n"
	@make retrieve_binding_sites GENE=MET3 min=-367 max=-349 GENE_OUT=test; mv test met4.bs
	@make retrieve_binding_sites GENE=MET3 min=-384 max=-366 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET14 min=-235 max=-217 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET16 min=-185 max=-167 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=ECM17 min=-311 max=-293 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=ECM17 min=-339 max=-321 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET10 min=-255 max=-237 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET10 min=-237 max=-219 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET2 min=-360 max=-342 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET2 min=-554 max=-536 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET17 min=-306 max=-288 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET17 min=-332 max=-314 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET6 min=-540 max=-522 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=MET6 min=-502 max=-484 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=SAM2 min=-329 max=-311 GENE_OUT=test; cat test >> met4.bs; rm test
	@make retrieve_binding_sites GENE=SAM2 min=-381 max=-363 GENE_OUT=test; cat test >> met4.bs; rm test


retrieve_all_binding_sites_for_met31:
	@echo "\nretrieve all binding sites for met31...\n"
	@make retrieve_binding_sites GENE=MET14 min=-202 max=-182 GENE_OUT=test; mv test met31.bs
	@make retrieve_binding_sites GENE=MET2 min=-313 max=-293 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET17 min=-227 max=-207 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET6 min=-313 max=-293 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=SAM2 min=-306 max=-286 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=SAM1 min=-283 max=-263 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET19 min=-173 max=-153 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MUP3 min=-188 max=-168 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET8 min=-184 max=-164 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET1 min=-232 max=-212 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET3 min=-259 max=-239 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET28 min=-159 max=-139 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET8 min=-434 max=-414 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET30 min=-168 max=-148 GENE_OUT=test; cat test >> met31.bs; rm test
#	@make retrieve_binding_sites GENE=MET6 min=-405 max=-385 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET14 min=-254 max=-233 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET14 min=-272 max=-251 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET14 min=-45 max=-24 GENE_OUT=test; cat test >> met31.bs; rm test
	@make retrieve_binding_sites GENE=MET14 min=-21 max=-1 GENE_OUT=test; cat test >> met31.bs; rm test

retrieve_all_binding_sites:
	@make retrieve_all_binding_sites_for_pho4
	@make retrieve_all_binding_sites_for_met4
	@make retrieve_all_binding_sites_for_met31

create_matrices_from_binding_sites:
	@for bs in ${BINDING_SITES} ; do \
		echo "create matrix $${bs}.matrice from binding sites $${bs}.bs ..." ; \
		consensus -f $${bs}.bs -L 15 -A a:t c:g -c2 | matrix-from-consensus -o $${bs}.matrice ; \
	done


################################################################################################
#  PATSER
################################################################################################

retrieve_all_seq:
	@echo "Retrieving all upstream sequences ..."
	@retrieve-seq -type upstream -org Saccharomyces_cerevisiae -from -800 -to -1 \
	-all -format wc -nocomments -label orf_gene -o yeast_all_up_orf.wc

patser_all_3top:
	@for matrix in ${MATRICES} ; do \
		echo "Matrix: $${matrix}.matrice" ; \
		patser -m $${matrix}.matrice -f yeast_all_up_orf.wc -A a:t c:g -c -ls 0 -t 3 \
		| features-from-patser -seq yeast_all_up_orf.wc -origin +800 -o $${matrix}_3top.ffp ; \
	done

match_sort_3top:
	@for matrix in ${MATRICES} ; do \
		echo "Sorting $${matrix}_3top.ffp" ; \
		grep -v ";" $${matrix}_3top.ffp | cut -f1,8 > test ; \
		./match-sort-3top test $${matrix}_3top.tab ; \
	done
	@rm test







################################################################################################
#  OLD VERSION
################################################################################################

all:
	make retrieve_each_seq
	make create_matrices
	make histo_each
	make patser_each
	make feature_map

purge_each_seq:
	for family in ${FAMILIES} ; do \
		echo "retrieving $${family} upstream sequences ..." ; \
		retrieve-seq -i $${family}.family -type upstream -org Saccharomyces_cerevisiae \
		-from -1 -to -800 -format fasta -nocomments -label orf_gene -o test ; \
		purge-sequence -i test -ml 200 -m 3 -o test2 ; \
		convert-seq -i test2 -from fasta -to wc -o $${family}.wc ; \
	done

histo_each:
	for matrix in ${MATRICES} ; do \
		for family in ${FAMILIES} ; do \
			echo "  Matrix: $${matrix}.matrice     Family: $${family} " ; \
			patser -m $${matrix}.matrice -f $${family}.wc -A a:t c:g -c \
			| features-from-patser -seq $${family}.wc -origin +800 \
			| cut -f 8 \
			| classfreq -ci 0.5 -max 16 -min -6 \
			|  XYgraph -o $${matrix}_$${family}.gif -histo -ycol 4 -xcol 3 -title1 "Frequency histogram" \
			-xsize 800 -ysize 500 -xgstep1 1 -xgstep2 0.5 \
			-xleg1 "score" -yleg1 "frequency" -xmin -5 -xmax 15 \
			-title2 "Matrix: $${matrix}, Family: $${family}" ;\
		done ; \
	done

cutoff_choice:
	@echo "Cutoff = score max de pho.matrices sur met.families et vice-versa" ;
	for family in ${FAMILIES_ALL} ; do \
		retrieve-seq -i $${family}.family -type upstream -org yeast -from -1 -to -800 \
		-format wc -nocomments -label orf_gene -o $${family}.wc ; \
		for matrix in ${MATRICES} ; do \
			echo "=====================================================" ; \
			echo "  Matrix: $${matrix}.matrice     Family: $${family} " ; \
			echo "=====================================================" ; \
			patser -m $${matrix}.matrice -f $${family}.wc -A a:t c:g -c -l 7 ; \
		done ; \
	done

cutoff_files:
	@echo "creating cutoff files ... "
	@echo 7.96 > pho4S.cutoff
	@echo 7.60 > pho4P.cutoff
	@echo 9.36 > met4P.cutoff
	@echo 8.22 > met31P.cutoff

patser_each:
	for matrix in ${MATRICES} ; do \
		for family in ${FAMILIES} ; do \
			echo "  Matrice: $${matrix}.matrice     Family: $${family} " ; \
			patser -m $${matrix}.matrice -f $${family}.wc -A a:t c:g -c -l `cat $${matrix}.cutoff` \
			| features-from-patser -seq $${family}.wc -origin +800 -o $${matrix}_$${family}.ffp ; \
		done ; \
	done

feature_map:
	for matrix in ${MATRICES} ; do \
		for family in ${FAMILIES} ; do \
			echo "  Matrice: $${matrix}.matrice     Family: $${family} " ; \
			feature-map -i $${matrix}_$${family}.ffp -legend -scorethick \
			-title "Matrix: $${matrix}, Family: $${family}" \
			-from -800 -to 0 -scalebar -scalestep 50 -htmap \
			-o $${matrix}_$${family}_map.gif > $${matrix}_$${family}_map.html ; \
		done ; \
	done 

oligo_analysis:
	for family in ${FAMILIES} ; do \
		echo "oligo-analysis of family $${family}.family ..." ; \
		oligo-analysis -i $${family}.wc -format wc -l 10 -2str  -return occ,proba -thosig 0 -sort ; \
	done



################################################################
## merge top scores obtained with the different matrices

TOP_FILES= pho4_3top.tab  pho4g_3top.tab pho4t_3top.tab met4_3top.tab met31_3top.tab 
TOP_TABLE=all_matrices_top3.tab
merge_top_scores:
	compare-scores 		-o ${TOP_TABLE} -files ${TOP_FILES}	\

